package com.weiyuwqj.server.auto;

import com.weiyuwqj.api.exception.HfException;
import com.weiyuwqj.api.model.*;
import com.weiyuwqj.api.service.BusAutoInvestService;
import com.weiyuwqj.api.util.AutoUtils;
import com.weiyuwqj.api.util.MyAssertUtils;
import com.weiyuwqj.server.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/18.
 */
@Component
public class BusItemAutoInvest {

    @Autowired
    private BusItemAutoMapper autoMapper;
    @Autowired
    private BusAutoInvestService autoService;
    @Autowired
    private BusAccountMapper accountMapper;
    @Autowired
    private BasItemMapper itemMapper;
    @Autowired
    BusAssetPackagePlanMapper busAssetPackagePlanMapper;
    @Autowired
    BasAssetsPackageMapper packageMapper;

//    /**
//     * 标匹配
//     *
//     * @param item
//     */
//    public void busItemMatchingAutoRule(BasItem item) {
//        Map map = getParamMap(item);
//        List<BusItemAuto> lists = autoMapper.selectListsByItem(map);
//        for (BusItemAuto auto : lists) {
//            if (BigDecimal.ZERO.compareTo(auto.getItemAmount()) == 0) continue;
//            try {
//                item = itemMapper.selectByPrimaryKey(item.getId());
//                BigDecimal investAmount = getInvestAmount(auto.getItemUserId(), auto.getItemAmount(),item);
//                if (investAmount.compareTo(BigDecimal.ZERO)<=0) continue;
//                if (investAmount.compareTo(item.getItemSingleMinInvestment())<=0) continue;
//                BigDecimal scale = autoService.scanAutoInvest(auto, auto.getItemUserId(),investAmount,item, null, 1);
//                if (scale.compareTo(new BigDecimal(100)) == 0) break;
//            } catch (HfException e) {
//                continue;
//            }
//        }
//    }

//    /**
//     * 债转匹配
//     *
//     * @param debt
//     */
//    public void basDebtMatchingAutoRule(BasDebt debt) {
//        BasItem item = itemMapper.selectByPrimaryKey(debt.getItemId());
//        Map map = getParamMap(item);
//        List<BusItemAuto> lists = autoMapper.selectListsByItem(map);
//        for (BusItemAuto auto : lists) {
//            BigDecimal investAmount = getInvestAmount(auto.getItemUserId(), auto.getItemAmount());
//            investAmount = AutoUtils.checkItemBlanceIsEnoughToInvest
//                    (investAmount, debt.getAmount().subtract(debt.getOngoingAmount()));
//            autoService.scanAutoInvest(auto, auto.getItemUserId(), investAmount, item, null, 2);
//        }
//
//    }

//    /**
//     * 包计划匹配
//     *
//     * @param plan
//     */
//    public void basAssetsPackageMatchingAutoRule(BusAssetPackagePlan plan) {
//
//        Integer packageId = plan.getAssetPackageId();
//        List<BasItem> items = itemMapper.getItemListByPackageId(packageId);
//        Map map = getParamMap(items.get(0));
//        BasAssetsPackage assetsPackage = packageMapper.selectByPrimaryKey(packageId);
//        MyAssertUtils.isNullOrEmpty(assetsPackage, "select_error");
//        List<BusItemAuto> lists = autoMapper.selectListsByItem(map);
//        for (BusItemAuto auto : lists) {
//            if (BigDecimal.ZERO.compareTo(auto.getItemAmount()) == 0) continue;
//            plan = busAssetPackagePlanMapper.getPlanByPackageIdAndPeriods(plan.getAssetPackageId(),plan.getPeriods());
//            BigDecimal onGoingAmount = plan.getOngoingAccount() == null ? BigDecimal.ZERO : plan.getOngoingAccount();
//            BigDecimal investAmount = getInvestAmount(auto.getItemUserId(), auto.getItemAmount());
//            investAmount = AutoUtils.checkItemBlanceIsEnoughToInvest(investAmount, plan.getAmount().subtract(onGoingAmount));
//            if (investAmount.compareTo(BigDecimal.ZERO)<=0) continue;
////            if (investAmount.compareTo()<=0) continue;
//            //份数
//            int part = investAmount.divide(assetsPackage.getEachAmount()).intValue();
//            investAmount = assetsPackage.getEachAmount().multiply(new BigDecimal(part));
//            try {
//                BigDecimal scale = autoService.scanAutoInvest(auto, auto.getItemUserId(), investAmount, null, plan, 3);
//                if (scale.compareTo(new BigDecimal(100)) == 0) break;
//            } catch (HfException e) {
//                continue;
//            }
//        }
//    }


    /**
     * 获取本次投资金额,如果账户余额不足规则金额，则取账户余额，否则取规则金额
     *
     * @param userId     用户id
     * @param autoAmount 规则金额
     * @return 本次投资金额
     * @throws NullPointerException
     */
    private BigDecimal getInvestAmount(Integer userId, BigDecimal autoAmount) {
        BusAccount account = accountMapper.getBusAccountByUserId(userId);
        MyAssertUtils.isNullOrEmpty(account, "select_error");
        return AutoUtils.getInvestAmount(account.getCash(), autoAmount);
    }

    /**
     * 获取标天数
     *
     * @param item
     * @return
     */
    private Map getParamMap(BasItem item) {
        MyAssertUtils.isNullOrEmpty(item, "select_error");
        Map map = new HashMap();
        switch (item.getItemCycle()) {
            case 1:
                map.put("itemDays", item.getItemCycle());  //天
                break;
            case 2:
                map.put("itemDays", item.getItemCycle() * 7);       //周
                break;
            case 3:
                map.put("itemDays", item.getItemCycle() * 30);      //月
                break;
            case 4:
                map.put("itemDays", item.getItemCycle() * 90);      //季
                break;
            case 5:
                map.put("itemDays", item.getItemCycle() * 360);     //年
                break;
            default:
                map.put("itemDays", 0);
        }
        return map;
    }

    /**
     * 获取投资金额
     *
     * @param userId
     * @param autoAmount
     * @param item
     * @return
     */
    private BigDecimal getInvestAmount(Integer userId, BigDecimal autoAmount,BasItem item) {
        BigDecimal investAmount = getInvestAmount(userId, autoAmount);
        investAmount = AutoUtils.checkItemBlanceIsEnoughToInvest(investAmount,
                item.getItemAccount().subtract(item.getItemOngoingAccount()));
        investAmount = investAmount.divide(new BigDecimal(100),0, RoundingMode.FLOOR).multiply(new BigDecimal(100)) ;
        return investAmount;
    }
}
